package com.foxit.pdf2html5.common.intergration;

import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.Resource;

/**
 * @author shaojie_zhuo
 * @version 1.0
 * Global app context of Foxit PDF Web Reader 
 */
public final class ReaderAppContext {
	 /**
	 * document management object.
	 */
	private static IDocStore docStore;
	
	/**
	 * User management object.
	 */
	private static IUserStore userStore;
	
	/**
	 * Cache management object.
	 */
	private static ICacheStoreProvider cacheStoreProvider;
	
	/**
	 * Configuration management object.
	 */
	private static IConfig config;
	
	
	/**
	 * initialize flag.
	 */
	private static boolean inited = false;
	
	
	private static ICacheManagement _cacheInterface;
	
	
	/**
	 * @param configXmlResource bean configuration
	 * @return return true if initialize successfully, otherwise return false.
	 */
	public static boolean InitContext(Resource configXmlResource)
    {
        if (!inited)
        {
        	DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        	BeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
        	reader.loadBeanDefinitions(configXmlResource);
            config = (IConfig)beanFactory.getBean("Config");
            docStore = (IDocStore)beanFactory.getBean("DocStore");
            userStore = (IUserStore)beanFactory.getBean("UserStore");
            cacheStoreProvider = (ICacheStoreProvider)beanFactory.getBean("ICacheStoreProvider");
            _cacheInterface = (ICacheManagement)beanFactory.getBean("CacheManagement");
        }
        inited = true;
        return true;
    }
	
	public static ICacheManagement getCacheInterface(){
		return _cacheInterface;
	}
	/**
	 * @return document management object.
	 */
	public static IDocStore DocStore()
	{
		return docStore;
	}
	
	/**
	 * @return User management object.
	 */
	public static IUserStore UserStore()
	{
		return userStore;
	}
	
	/**
	 * @return Cache management object.
	 */
	public static ICacheStoreProvider CacheStoreProvider()
	{
		return cacheStoreProvider;
	}
	
	/**
	 * @return Configuration management object.
	 */
	public static IConfig Config()
	{
		return config;
	}
}
